
package com.clyme.expediente.hibernate;

import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Luis Alonso Ballena Garcia
 */
public class HibernateConexion implements IConexionHibernate{
    private Session session = null;
    private Transaction tx = null;
    
    @Override
    public void beginConexion() {
        if(session == null || !session.isOpen()){
            session = HibernateUtil.getSessionFactory().openSession();
        }
        
    }

    @Override
    public void closeConexion() {
        if(session != null){
            session.close();
        }
    }

    @Override
    public void beginTransaccion() {
        beginConexion();
        if(tx == null){
             tx = session.beginTransaction();
        }
    }

    @Override
    public void closeTransaccion(){
        try{
            if(tx != null){
                tx.commit();
            }
        }catch(Exception e){
            tx.rollback();
            e.printStackTrace();
        }finally{
            tx = null;
            closeConexion();
        }
    }

    @Override
    public Session getSession() {
        return session;
    }
    
    
}
